/* Copyright 2008 Shawn C. Dodd

This file is part of TagFC.  TagFC is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

TagFC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
 
You should have received a copy of the GNU Affero General Public License along with TagFC.  If not, see <http://www.gnu.org/licenses/>. */
 
 class NicknameController {
	def scaffold = Nickname

	def supply = {
		// blank form is a request to log out
		if( !params.nickname ) {
			session.removeAttribute("nickname")
			return redirect(uri:"/",action:"index")
		}

		// if nickname exists, log 'em in; otherwise, create a new nickname and warn
		def n = params.nickname
		def o = Nickname.findByDisplayName(n)
		def dest = [url:params.backUri]
		if( o ) {
			session.setAttribute("nickname", n)
		} else {
			def d = new Nickname()
			d.displayName = n
			d.save()
			o = d
			session.setAttribute("nickname", d.displayName)
			flash.message = "Note: New account created for ${d.displayName}."
		}
		if( dest =~ /v1\/work\/set/ ) {
			dest = [controller:"workset",action:"work"]
		}

		return redirect(dest)
	}

//    def index = { }
}
